/* TCSS 590A - Cybernetics and Systems
 * Group Project - Modeling
 * By Barry Jones, Naren Meadem, Michael Pitts, Yuan Tao
 * Fall 2012, University of Washington, Tacoma
 */
package output;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Generates OutPut objects based on given command line options.
 * @author Michael Pitts
 * @version Nov 8, 2012
 */
public class OutPutGenerator
{
	public static OutPut generateOutPutCollection(final String[] the_args) 
			throws FileNotFoundException
	{
		return new OutPutCollection(generateOutPut(the_args));
	}
	
	/**
	 * Generates one or more OutPut objects based on command line arguments.
	 * @param the_args are the arguments from the command line.
	 * @return an array of OutPut objects to be used.
	 * @throws FileNotFoundException if the indicated file is not found.
	 */
	public static OutPut[] generateOutPut(final String[] the_args)
			throws FileNotFoundException
	{
		if (the_args.length > 0)
		{
			final Collection<OutPut> outputs = new ArrayList<OutPut>();
			OutPut temp; // temporarily holds potential outputs
			for (int i = 0; i< the_args.length; i++)
			{
				if (the_args[i].startsWith(OutPut.IN)) 
				{
					temp = getOutPut(the_args, i);
					if (temp != null) outputs.add(temp);
				}
			}
			if (outputs.size() > 0) // if some output command
				return outputs.toArray(new OutPut[outputs.size()]);
		} // otherwise return a default output to console
		return new OutPut[] {new OutPutStream(System.out, OutPut.NEWLINE, OutPut.DELINEATOR)};
	}
	
	/**
	 * Generates a specific output from the given String argument.
	 * @param the_args are the command line arguments.
	 * @param the_start is what index in the_args to examine.
	 * @return a new OutPut object, or null if one could not be formated from the
	 * given arguments.
	 * @throws FileNotFoundException if the indicated file is not found.
	 */
	private static OutPut getOutPut(final String[] the_args, final int the_start) 
			throws FileNotFoundException
	{
		if (the_args[the_start].startsWith(OutPut.FILE) && the_start+1 < the_args.length)
			return setUpStream(new PrintStream(the_args[the_start+1]), the_args, the_start+2);
		else if (the_args[the_start].startsWith(OutPut.CONSOLE))
			return setUpStream(System.out, the_args, the_start+1);
		else 
			return null; // not an output command
	}

	/**
	 * Sets up a new OutPutStream, using the indicated stream as the core.
	 * @param the_stream is the PrintStream to wrap.
	 * @param the_args are the command line arguments which can indicate settings.
	 * @param the_index is the index on the_args where settings might begin.
	 * @return a new OutPutStream, with indicated settings or the default.
	 */
	private static OutPut setUpStream(final PrintStream the_stream, 
			final String[] the_args, final int the_index)
	{
		String new_line = OutPut.NEWLINE,  del = OutPut.DELINEATOR;
		if (the_index < the_args.length && !the_args[the_index].startsWith(OutPut.IN))
			new_line = the_args[the_index];
		if (the_index+1 < the_args.length && !the_args[the_index+1].startsWith(OutPut.IN))
			del = the_args[the_index+1];
		return new OutPutStream(the_stream, new_line, del);
	}
}
